import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login/index.vue'
import Home from '@/components/Home/index.vue'
import Welcome from '@/components/Welcome/index.vue'
import User from '@/components/User/index.vue'
import Rights from '@/components/Power/rights.vue'
import Roles from '@/components/Power/Roles.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [{
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: User
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 1.登录页直接放行
  if (to.path == '/login') {
    next()
  } else {
    const token = sessionStorage.getItem('TOKEN')
    if (!token) {
      next('/login')
    } else {
      next()
    }
  }
})

export default router
